#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


#class MainHandler(webapp.RequestHandler):
#    def get(self):
#        self.response.out.write('Hello world!')


from google.appengine.ext import db
import os
from google.appengine.ext.webapp import template
import cgi
from google.appengine.api import users

###
class Scene(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


###
class AddScene(webapp.RequestHandler):
  def post(self):
    scene=Scene()

    if users.get_current_user():
      scene.author = users.get_current_user()

    scene.content = self.request.get('content')
    scene.put()

    self.redirect('/')

###
class SelectScene(webapp.RequestHandler):
  def get(self):
 
    user = users.get_current_user()

    if user:
      pass
    else:
      self.redirect(users.create_login_url(self.request.uri))

    scene=Scene()

    scenes=scene.gql("WHERE author = :1 ORDER BY date DESC LIMIT 32",user)

    logout_url = users.create_logout_url(self.request.uri)

    template_values = {
      'scenes': scenes,
      'logout_url': logout_url,
      }
    path = os.path.join(os.path.dirname(__file__), 'scene.html')
    self.response.out.write(template.render(path, template_values))

###
class DelScene(webapp.RequestHandler):
  def post(self):
    scene=Scene()
    item=Item()

    dscene=scene.gql("WHERE author = :1 and content = :2 ",
	users.get_current_user(),self.request.get('selected'))

    db.delete(dscene)

    ditem=item
    items=item.gql("WHERE author = :1 and scene = :2 ",
	users.get_current_user(),self.request.get('selected'))

    db.delete(items)

    self.redirect('/')


###
class Item(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  scene = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


###
class AddItem(webapp.RequestHandler):
  def post(self):
    item=Item()

    if users.get_current_user():
      item.author = users.get_current_user()

    item.scene = self.request.get('scene')
    item.content = self.request.get('content')
    item.put()

    self.redirect('/')

###
class CheckItem(webapp.RequestHandler):
  def post(self):
 
    user = users.get_current_user()

    if user:
      pass
    else:
      self.redirect(users.create_login_url(self.request.uri))

    scene = self.request.get('selected')

    item=Item()

    items=item.gql("WHERE author = :1 and scene = :2 ORDER BY date DESC LIMIT 32",user,scene)
 
    logout_url = users.create_logout_url(self.request.uri)

    template_values = {
      'items': items,
      'scene': scene,
      'logout_url': logout_url,
      }
    path = os.path.join(os.path.dirname(__file__), 'item.html')
    self.response.out.write(template.render(path, template_values))




def main():

    application = webapp.WSGIApplication([('/', SelectScene),
                                          ('/addScene', AddScene),
                                          ('/delScene', DelScene),
                                          ('/checkItem', CheckItem),
                                          ('/addItem', AddItem)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
